/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var username;

//WEBSOCKET
var wsUri = "ws://localhost:8080/ChatYo/chat";
var websocket = new WebSocket(wsUri);
websocket.onopen = function(evt) { onOpen(evt); };
websocket.onmessage = function(evt) { onMessage(evt); };
websocket.onerror = function(evt) { onError(evt); };

//JSON 
var coordJSON ={"coordenadas":[
        {"xlancha":30},
        {"ylancha":40},
        {"xbarco":100},
        {"ybarco":100}]};

var msg;
window.onload = function() { document.onkeypress = mostrarInformacionTecla;
document.onkeyup = mostrarInformacionTecla; };

function mostrarInformacionTecla(evObject) {
                msg = ''; var teclaPulsada = evObject.keyCode;
          switch(teclaPulsada){
                    case 37:
                            msg = 'izq';
                            break;
                    case 38:
                              msg = 'arriba';
                            break;
                    case 39:
                              msg = 'der';
                            break;
                    case 40:
                            msg = 'abajo';
                            break;
            }
}

function join() {
    username = textField.value;
    websocket.send(username + " ---joined");
}

function send_message() {
    websocket.send(coordJSON.coordenadas[0].xlancha); 
    //mando solo una coordenada, pero parseando debo poder mandar el JSON entero
  }

function onOpen() {
    writeToScreen("Conecto al wsocket"); //Connected to " + wsUri);
}

function onMessage(evt) {
    console.log("onMessage: " + evt.data);
    if (evt.data.indexOf("joined") !== -1) {
        userField.innerHTML += evt.data.substring(0, evt.data.indexOf(" joined")) + "\n";
    } else {
      chatlogField.innerHTML += evt.data + "\n"; //aca hay que mandar el json para capturarlo en el canvas
    }
  

}

function onError(evt) {
    writeToScreen('<span style="color: red;">ERROR1:</span> ' + evt.data);
}

function writeToScreen(message) {
    content.innerHTML += message + "<br>";
}